﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Modules_Security_ManageUsers : System.Web.UI.Page
{
    public string UserNameCurrent
    {
        get
        {
            if (ViewState["UserNameCurrent"] != null)
            {
                return ViewState["UserNameCurrent"].ToString();
            }
            return string.Empty;
        }
        set
        {
            ViewState["UserNameCurrent"] = value;

        }
    }
    public string Alphabet
    {
        get
        {
            if (ViewState["Alphabet"] != null)
            {
                return ViewState["Alphabet"].ToString();
            }
            return string.Empty;
        }
        set
        {
            ViewState["Alphabet"] = value;
        }
    }
    public int ChoiceDataSource
    {
        get
        {
            if (ViewState["ChoiceDataSource"] != null)
            {
                return int.Parse(ViewState["ChoiceDataSource"].ToString());
            }
            return 0;
        }
        set
        {
            ViewState["ChoiceDataSource"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] alphabet = "A;B;C;D;E;F;G;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;All".Split(';');
            AlphabetRepeater.DataSource = alphabet;
            AlphabetRepeater.DataBind();
            BindGrid();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        BindGrid1();
    }
    void BindGrid()
    {
        MembershipUserCollection userc = Membership.GetAllUsers();
        DataGrid.DataSource = userc;
        DataGrid.DataBind();
        ChoiceDataSource = 1;
    }
    void BindGrid1()
    {

        string search = "";
        if (TextBox1.Text != "")
            search = TextBox1.Text;
        else
            search = "%%";
        MembershipUserCollection userc = null;
        if (SearchByDropDown.SelectedValue.Equals("E-mail"))
            userc = Membership.FindUsersByEmail(search);
        else
            userc = Membership.FindUsersByName(search);

        DataGrid.DataSource = userc;
        DataGrid.DataBind();
        ChoiceDataSource = 2;
    }
    void BindGrid2(string keywork)
    {
        MembershipUserCollection userc = null;
        if (SearchByDropDown.SelectedValue.Equals("E-mail"))
        {
            if (keywork.ToString() == "All")
                userc = Membership.FindUsersByEmail("%%");
            else
                userc = Membership.FindUsersByEmail(keywork + "%");
        }
        else
        {
            if (keywork.ToString() == "All")
                userc = Membership.FindUsersByName("%%");
            else
                userc = Membership.FindUsersByName(keywork + "%");
        }
        DataGrid.DataSource = userc;
        DataGrid.DataBind();
        ChoiceDataSource = 3;
    }
    public void IndexChanged(object sender, GridViewPageEventArgs e)
    {
        DataGrid.PageIndex = e.NewPageIndex;
        if (ChoiceDataSource == 1)
            BindGrid();
        else if (ChoiceDataSource == 2)
            BindGrid1();
        else
            BindGrid2(Alphabet);
    }
    public void RetrieveLetter(object sender, RepeaterCommandEventArgs e)
    {
        Alphabet = e.CommandArgument.ToString();
        BindGrid2(Alphabet);
    }
    public void EnabledChanged(object sender, EventArgs e)
    {
        CheckBox checkBox = (CheckBox)sender;
        GridViewRow item = (GridViewRow)checkBox.Parent.Parent;
        Label label = (Label)item.FindControl("UserNameLink");
        string userID = label.Text;
        MembershipUser user = Membership.GetUser(userID);
        user.IsApproved = checkBox.Checked;
        Membership.UpdateUser(user);
    }
    public void LinkButtonClick(object sender, CommandEventArgs e)
    {
        string CurrentUser;
        if (e.CommandName.Equals("EditUser"))
        {
            CurrentUser = ((string)e.CommandArgument);
            // do not prepend ~/ to this path since it is not at the root
            Response.Redirect("AdminUpdateProfile.aspx?UID=" + CurrentUser);
        }
        if (e.CommandName.Equals("DeleteUser"))
        {
            CurrentUser = (string)e.CommandArgument;
            Membership.DeleteUser(CurrentUser, true);
            BindGrid();
        }
    }
    public void ButtonClick(object sender, EventArgs e)
    {
        LinkButton button = (LinkButton)sender;
        UserNameCurrent = button.CommandArgument;
        string[] str = Roles.GetAllRoles();

        CheckBoxRepeater.DataSource = str;
        CheckBoxRepeater.DataBind();
        multiView1.ActiveViewIndex = 1;
    }
    public bool UserIsRoles(object RoleName)
    {
        return Roles.IsUserInRole(UserNameCurrent, RoleName.ToString());
    }
    protected void RoleMembershipChanged(object sender, EventArgs e)
    {
        try
        {
            CheckBox box = (CheckBox)sender;
            // Array manipulation because cannot use Roles static method (need different appPath).
            string role = box.Text;


            if (box.Checked)
            {
                Roles.AddUserToRole(UserNameCurrent, role);
            }
            else
            {
                Roles.RemoveUserFromRole(UserNameCurrent, role);
            }
        }
        catch
        {
            // Ignore, e.g., user is already in role.
        }
    }

    protected void DataGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {


    }

    protected void DataGrid_DataBound(object sender, EventArgs e)
    {
        //Button DeleteButton = (Button)DataGrid.Rows[5].Cells[0].Controls[2];
        //if (DeleteButton != null)
        //{
        //    DeleteButton.OnClientClick = "if (!confirm('Bạn có " +
        //               "chắc muốn xóa câu hỏi này?')) return;";
        //}


    }
}